package com.ruoyi.project.module.project.controller;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.module.utils.ProjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.service.IProjectService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 接口项目 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-09-28
 */
@Controller
@RequestMapping("/module/project")
public class ProjectController extends BaseController
{
    private String prefix = "module/project";
	
	@Autowired
	private IProjectService projectService;
	
	@RequiresPermissions("module:project:view")
	@GetMapping()
	public String project()
	{
	    return prefix + "/project";
	}

	@RequiresPermissions("module:project:view")
	@GetMapping("/uniqueid/{uniqueid}")
	public String project(@PathVariable("uniqueid") Integer uniqueid, ModelMap mmap)
	{
	    if(uniqueid!=null){
			Project project = projectService.selectProjectById(uniqueid);
			if(project!=null) {
				String ancestors = project.getAncestors();
				String[] split = ancestors.split(",");
				if(split.length>0){
					if(StringUtils.isBlank(split[0])){
						split[0]="0";
					}
				}
				mmap.put("uniqueid" , uniqueid);
				mmap.put("ancestors",split);
			}
		}
		return prefix + "/project";
	}

	/**
	 * 查询接口项目列表
	 */
	@RequiresPermissions("module:project:list")
	@GetMapping("/listtree")
	@ResponseBody
	public List<Project> listtree(Project project)
	{
		List<Project> list = projectService.selectProjectList(project);
		return (list);
	}

	/**
	 * 查询接口项目列表
	 */
	@RequiresPermissions("module:project:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Project project)
	{
		startPage();
        List<Project> list = projectService.selectProjectList(project);
		return getDataTable(list);
	}

	/**
	 * 新增接口项目
	 */
	@GetMapping("/add/{parentId}")
	public String addByParentid(@PathVariable("parentId") Integer parentId, ModelMap mmap)
	{
		Project info = projectService.selectProjectById(parentId);
		if(info==null){
			Project project = new Project();
			project.setApiProjectnameId(0);
			mmap.put("parent", project);
			mmap.put("maxOrderNum", 10);
		}else{
			int maxOrderNum = projectService.maxOrderNum(parentId.longValue());
			mmap.put("maxOrderNum", maxOrderNum+10);
			mmap.put("parent", info);
		}
		return prefix + "/add";
	}


	Pattern p=Pattern.compile("^[A-Za-z0-9\\-_]+$");

	/**
	 * 新增保存接口项目
	 */
	@RequiresPermissions("module:project:add")
	@Log(title = "接口项目", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Project project)
	{
	    if(StringUtils.isBlank(project.getDescription())){
			return error(1, "描述不允许为空");
		}
	    if(!p.matcher(project.getProjectname()).find()){
			return error(1, "[项目名称]只允许英文大小写,数字和'_-'字符");
		}


		return toAjax(projectService.insertProject(project));
	}

	/**
	 * 修改接口项目
	 */
	@GetMapping("/edit/{apiProjectnameId}")
	public String edit(@PathVariable("apiProjectnameId") Integer apiProjectnameId, ModelMap mmap)
	{
		Project project = projectService.selectProjectById(apiProjectnameId);
		mmap.put("project", project);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存接口项目
	 */
	@RequiresPermissions("module:project:edit")
	@Log(title = "接口项目", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Project project)
	{
		Project project1 = projectService.selectProjectById(project.getApiProjectnameId());
		Project project2 = projectService.selectProjectById(project.getParentId());
		if(project2!=null){
			if(project1.getAncestors().equals(project2.getAncestors())){
				//同一层级,ID相同,说明同一条记录
				if(project1.getApiProjectnameId().equals(project2.getApiProjectnameId())){
					return error(1, "'上级项目'不允许为记录本身或者记录子级");
				}else{
					return toAjax(projectService.updateProject(project));
				}
			}
			if(project2.getAncestors().indexOf(project1.getAncestors())>-1){
				return error(1, "'上级项目'不允许为记录本身或者记录子级");
			}
		}

		return toAjax(projectService.updateProject(project));
	}
	
	/**
	 * 删除接口项目
	 */
	@RequiresPermissions("module:project:remove")
	@Log(title = "接口项目", businessType = BusinessType.DELETE)
	@PostMapping( "/remove/{apiProjectnameId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("apiProjectnameId") Integer apiProjectnameId)
	{		
		return toAjax(projectService.deleteProjectById(apiProjectnameId));
	}

	/**
	 * 加载项目列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData()
	{
		List<Map<String, Object>> tree = projectService.selectProjectTree();
		return tree;
	}

	/**
	 * 选择部门树
	 */
	@GetMapping("/selectProjectTree/{id}")
	public String selectProjectTree(@PathVariable("id") Integer id, ModelMap mmap)
	{
		mmap.put("project", projectService.selectProjectById(id));
		return prefix + "/tree";
	}



	@PostMapping("/getErrorGroupByProjectId/{projectId}")
	@ResponseBody
	public  Integer getErrorGroupByProjectId(@PathVariable("projectId") Integer projectId){

		Project project = projectService.selectProjectById(projectId);
		Integer errorGroup = ProjectUtils.getErrorGroupByProject(projectService, project);
		if(errorGroup==null){
		    return -1;
        }
		return errorGroup;

	}


	@PostMapping("/hasChilds")
	@ResponseBody
	public Boolean hasChilds(Integer apiProjectnameId){
		return projectService.hasChilds(apiProjectnameId);
	}

}
